package yitu.com.accessibledemo2;

import android.accessibilityservice.AccessibilityService;
import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import java.util.List;

public class MyAccessibilityService extends AccessibilityService {
    private static final String TAG = "MyAccessibilityService";

    @Override
    protected void onServiceConnected() {
        Log.i(TAG, "onServiceConnected");
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        // TODO Auto-generated method stub
        Log.i(TAG, " eventType " + event.getEventType());

        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();


        String pkgName = event.getPackageName().toString();
        if ("yitu.com.accessibledemo2".equals(pkgName)) {
            Log.e("MyAccessibilityService", "MyAccessibilityService onAccessibilityEvent()yitu.com.accessibledemo2");
            if (nodeInfo != null) {

                List<AccessibilityNodeInfo> list =
                        nodeInfo.findAccessibilityNodeInfosByText("蓝牙");

                if (list.size() > 0) {
                    list.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }
            }
        } else if ("com.android.settings".equals(pkgName)) {
            if (nodeInfo != null) {
                List<AccessibilityNodeInfo> list1 = nodeInfo.findAccessibilityNodeInfosByText("权限管理");

                if (list1!=null) {
                    if (list1.size()>0) {
                        AccessibilityNodeInfo accessibilityNodeInfo = list1.get(0);

                        accessibilityNodeInfo.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    }
                }

//                list1 = nodeInfo.findAccessibilityNodeInfosByText("锁屏显示");
//                if (list1!=null) {
//                    if (list1.size()>2) {
//                        AccessibilityNodeInfo accessibilityNodeInfo = list1.get(1);
//
//                        accessibilityNodeInfo.getParent().getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                    }
//                }
//
                list1 = nodeInfo.findAccessibilityNodeInfosByText("允许应用在锁屏上显示");
                if (list1!=null) {
                    Log.e("MyAccessibilityService", "允许应用在锁屏上显示 onAccessibilityEvent()"+list1);
                    if (list1.size()>0) {
                        AccessibilityNodeInfo accessibilityNodeInfo = list1.get(0);
                        accessibilityNodeInfo.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    }
                }
                list1 = nodeInfo.findAccessibilityNodeInfosByText("允许");
                if (list1!=null) {
                    if (list1.size()>0) {
                        AccessibilityNodeInfo accessibilityNodeInfo = list1.get(0);
                        accessibilityNodeInfo.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    }
                }

//
//                List<AccessibilityNodeInfo> nodeInfosByClassName = nodeInfo.findAccessibilityNodeInfosByViewId("android:id/list");
//                if (nodeInfosByClassName != null && nodeInfosByClassName.size() > 0) {
//                    AccessibilityNodeInfo accessibilityNodeInfo = nodeInfosByClassName.get(0);
//                    if (accessibilityNodeInfo.getChildCount() >= 3) {
//                        AccessibilityNodeInfo child = accessibilityNodeInfo.getChild(1);
//                        if (child != null) {
//                            Log.e("MyAccessibilityService", "MyAccessibilityService onAccessibilityEvent()"+child);
//                            child.performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                        }
//                    }
//
//                }

            }

        } else if ("....".equals(pkgName)) {

        }

        int eventType = event.getEventType();
        switch (eventType) {
            case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
                //.......
        }
    }

    @Override
    public void onInterrupt() {
        // TODO Auto-generated method stub
        Log.i(TAG, "onServiceConnected");
    }

}


//    public MyAccessibilityService() {
//    }
//
//
//    @Override
//    public void onInterrupt() {
//
//    }
//
//    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
//    @Override
//    protected void onServiceConnected() {
//        AccessibilityServiceInfo info = getServiceInfo();
//        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
//        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
//        info.notificationTimeout = 100;
//        setServiceInfo(info);
//        info.packageNames = new String[]{"xxx.xxx.xxx", "yyy.yyy.yyy", "...."};
//        setServiceInfo(info);
//        super.onServiceConnected();
//    }
//
//
//    @Override
//    public void onAccessibilityEvent(AccessibilityEvent event) {
//        String pkgName = event.getPackageName().toString();
//        if("xxx.xxx.xxx".equals(pkgName)){
//
//        }else if("yyy.yyy.yyy".equals(pkgName)){
//
//        }else if("....".equals(pkgName)){
//
//        }
//
//        int eventType = event.getEventType();
//        switch (eventType) {
//            case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:
//                //.......
//        }
//    }

